package com.ipzoe.web.controller.act;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.ipzoe.common.utils.SecurityUtils;
import com.ipzoe.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ipzoe.common.annotation.Log;
import com.ipzoe.common.core.controller.BaseController;
import com.ipzoe.common.core.domain.AjaxResult;
import com.ipzoe.common.enums.BusinessType;
import com.ipzoe.act.domain.Activity;
import com.ipzoe.act.service.IActivityService;
import com.ipzoe.common.utils.poi.ExcelUtil;
import com.ipzoe.common.core.page.TableDataInfo;

/**
 * 活动Controller
 * 
 * @author eric
 * @date 2020-08-17
 */
@RestController
@RequestMapping("/act/activity" )
public class ActivityController extends BaseController {

    @Autowired
    private IActivityService iActivityService;

    /**
     * 查询活动列表
     */
    @PreAuthorize("@ss.hasPermi('act:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(Activity activity)
    {
        startPage();
        LambdaQueryWrapper<Activity> lqw = new LambdaQueryWrapper<Activity>();
        if (StringUtils.isNotBlank(activity.getName())){
            lqw.like(Activity::getName ,activity.getName());
        }
        if (activity.getStatus() != null){
            lqw.eq(Activity::getStatus ,activity.getStatus());
        }
        lqw.orderByDesc(Activity::getCreateTime);
        List<Activity> list = iActivityService.list(lqw);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('act:activity:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(){
        LambdaQueryWrapper<Activity> lqw = new LambdaQueryWrapper<Activity>();
        lqw.orderByDesc(Activity::getCreateTime);
        return AjaxResult.success(iActivityService.list(lqw));
    }

    /**
     * 导出活动列表
     */
    @PreAuthorize("@ss.hasPermi('act:activity:export')" )
    @Log(title = "活动" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Activity activity) {
        LambdaQueryWrapper<Activity> lqw = new LambdaQueryWrapper<Activity>(activity);
        List<Activity> list = iActivityService.list(lqw);
        ExcelUtil<Activity> util = new ExcelUtil<Activity>(Activity. class);
        return util.exportExcel(list, "activity" );
    }

    /**
     * 获取活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('act:activity:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iActivityService.getById(id));
    }

    /**
     * 新增活动
     */
    @PreAuthorize("@ss.hasPermi('act:activity:add')" )
    @Log(title = "活动" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Activity activity) {

        activity.setCreateBy(SecurityUtils.getUsername());
        Date today = new Date();

        Date beginOfDayBeginTime = DateUtil.beginOfDay(activity.getBeginTime());
        Date beginOfDayEndTime = DateUtil.beginOfDay(activity.getEndTime());
        Date beginOfDayToday = DateUtil.beginOfDay(today);
        if(beginOfDayToday.before(beginOfDayBeginTime)){
            activity.setStatus(0);
        }else if(beginOfDayToday.after(beginOfDayEndTime)){
            activity.setStatus(2);
        }else{
            activity.setStatus(1);
        }
        return toAjax(iActivityService.save(activity) ? 1 : 0);
    }

    /**
     * 修改活动
     */
    @PreAuthorize("@ss.hasPermi('act:activity:edit')" )
    @Log(title = "活动" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Activity activity) {

        activity.setUpdateBy(SecurityUtils.getUsername());
        Date now = new Date();
        if(DateUtil.between(activity.getBeginTime(), now, DateUnit.HOUR,false)>=0 && DateUtil.between(now, activity.getEndTime(), DateUnit.HOUR,false)>=0) {
            activity.setStatus(1);
        }else if(DateUtil.between(activity.getBeginTime(), now, DateUnit.HOUR,false)<0){
            activity.setStatus(0);
        }else{
            activity.setStatus(2);
        }
        return toAjax(iActivityService.updateById(activity) ? 1 : 0);
    }

    /**
     * 删除活动
     */
    @PreAuthorize("@ss.hasPermi('act:activity:remove')" )
    @Log(title = "活动" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iActivityService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
